package com.gakj.truck.third.api.service;


import com.alibaba.fastjson.JSONObject;
import com.gakj.truck.third.api.mapper.UserOumanMapper;
import com.gakj.truck.third.api.ouman.OumanService;
import com.gakj.truck.third.core.domain.CustomorDto;
import com.gakj.truck.third.core.domain.UserDto;
import com.gakj.truck.third.core.domain.UserOuman;
import org.apache.commons.lang.NullArgumentException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserTmpService {

    private static final String NOT_SAVE = "0";
    private static final String HAS_SAVE = "1";

    private static final String SOUCE_OUMAN="0";

    private static final Logger LOGGER = LoggerFactory.getLogger(UserTmpService.class);

    @Autowired
    UserOumanMapper oumanMapper;

    @Autowired
    OumanService oumanService;

    public JSONObject checkOumanUser(String username,String pwd,String tel){

        JSONObject result = new JSONObject();

        JSONObject object = oumanService.CheckUser(username, tel, pwd);
        if(object!=null && object.size()>0){
//            JSONObject data = object.getJSONObject("data");
            String data = object.getString("data");
//            JSONArray data = object.getJSONArray("data");
            if(StringUtils.isNotEmpty(data)){
//                saveOrUpdate(data.toString());
                LOGGER.info("正确返回数据 : "+data);
                result.put("resMsg","欧曼用户存在!");
                result.put("resCode","1");
                result.put("data",data);
            }else{
                result.put("resMsg","欧曼用户不存在!");
                result.put("resCode","0");
                result.put("data",new JSONObject());
            }
        }else{
            result.put("resMsg","请求欧曼数据失败!");
            result.put("resCode","0");
        }

        return result;
    }


    public List<UserOuman> getAll() {
        return oumanMapper.getAll();
    }

    public void saveOrUpdate(String jsonString) {
        if (StringUtils.isBlank(jsonString)) {
            throw new NullArgumentException("传输数据错误!");
        }

        JSONObject data = JSONObject.parseObject(jsonString);
        UserOuman ou = transform(data);
        Long oumanid = ou.getOumanid();
        Long newId;
        UserOuman userOuman = getByoumanid(oumanid);
        if (userOuman == null) {
            ou.setSaveStatus(NOT_SAVE);
            ou.setCreatetime(new Date());
            ou.setUpdatetime(new Date());
            ou.setSource(SOUCE_OUMAN);
            save(ou); //TODO 保存到临时数据库中
            newId = ou.getId();
        } else {
            newId = userOuman.getId();
            copyProperties(ou,userOuman);
            userOuman.setUpdatetime(new Date());
            update(userOuman);
        }
        UserOuman ouman = getByid(newId);
//
//        try {
//            saveOrUpdateToSailDB(ouman);
//            LOGGER.info("同步到蜗牛数据库成功!");
//        }catch (Exception e){
//            e.printStackTrace();
//            LOGGER.error("同步到蜗牛数据库失败!");
//        }

    }

    private void saveOrUpdateToSailDB(UserOuman userOuman) {

        String companyname = userOuman.getCompanyname();
        String isjoin = userOuman.getIsjoin();
        String licencePhoto = userOuman.getLicencePhoto();
        String loginName = userOuman.getLoginName();
        String name = userOuman.getName();
        String sex = userOuman.getSex();
        String status = userOuman.getStatus();
        String mobile = userOuman.getTel();
        //保存到sht_usr
        UserDto userdto = new UserDto();
        userdto.setLoginName(loginName);
        userdto.setName(name);


        CustomorDto customorDto = new CustomorDto();


    }

    public UserOuman getByoumanid(Long oumanid){
        return oumanMapper.getbyOumanid(oumanid);
    }

    public UserOuman getByid(Long id){
        return oumanMapper.getById(id);
    }

    public void save(UserOuman userOuman) {
        oumanMapper.save(userOuman);
    }

    public void update(UserOuman userOuman){
        oumanMapper.update(userOuman);
    }


    private void copyProperties(UserOuman source,UserOuman target){
        target.setName(source.getName());
        target.setSex(source.getSex());
        target.setLoginName(source.getLoginName());
        target.setStatus(source.getStatus());
        target.setTel(source.getTel());
        target.setIsjoin(source.getIsjoin());
        target.setCompanyname(source.getCompanyname());
        target.setLicencePhoto(source.getLicencePhoto());
    }
    /**
     * 把JSONObject转换成对象
     *
     * @param json
     * @return
     */
    public UserOuman transform(JSONObject json) {

        Long id = json.getLong("id");
        String name = json.getString("name");
        String loginName = json.getString("loginname");
        String tel = json.getString("tel");
        String sex = json.getString("sex");
        String status = json.getString("status");
        String isjoin = json.getString("isjoin");
        String companyname = json.getString("companyname");
        String licencePhoto = json.getString("licencephoto");


        UserOuman userOuman = new UserOuman();

        userOuman.setOumanid(id);
        userOuman.setName(name);
        userOuman.setLoginName(loginName);
        userOuman.setTel(tel);
        userOuman.setStatus(status);
        userOuman.setSex(sex);
        userOuman.setCompanyname(companyname);
        userOuman.setIsjoin(isjoin);
        userOuman.setLicencePhoto(licencePhoto);
        return userOuman;
    }
}
